<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Links extends CI_Controller {
	
	private $tab = 'site';
	
	function __construct(){
		parent::__construct();
		/** 登陆验证 */
		if(!isset($_SESSION)){
		    session_start();
		}
		$admin = $this->session->userdata('admin_user');
		if(!$admin || !isset($admin['user_id'])){
			header('Location: /ychadmin/login');
			exit;
		}
	}
	
	function add_view(){
		$this->load->helper(array('form'));
		
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'links_add';
		
		$this->load->view("/ychadmin/links_add",$vdata);
	}
	
	function add(){
		$type 		= $this->input->post('type');
		$url 		= $this->input->post('url');
		$webname 	= $this->input->post('webname');
		$forder 	= $this->input->post('forder');
		//如果有上传图片，则先完成图片上传
		if(!file_exists(UPFILES_LINKS_IMG)){
			mkdir(UPFILES_LINKS_IMG);
		}
		$logoimg = "";
		if($_FILES['logoimg']['name'] != '')
		{ 
			$logoimg = sha1($_SERVER['HTTP_USER_AGENT'].$_SERVER['REMOTE_ADDR'].time().rand());
			$tmpfn = $_FILES['logoimg']['name'];
			$pt=strrpos($tmpfn, ".");
			$retval=substr($tmpfn, $pt+1, strlen($tmpfn) - $pt);
			$logoimg.=".".$retval;
			$upfile = UPFILES_LINKS_IMG."/".$logoimg;
			if(is_uploaded_file($_FILES['logoimg']['tmp_name']))
				move_uploaded_file($_FILES['logoimg']['tmp_name'],$upfile);
		}
		
		
		$this->load->helper(array('form'));
		$this->load->library('form_validation');
		$this->form_validation->set_rules('url', 				'标题', 		'trim|min_length[1]|max_length[100]');	
		$this->form_validation->set_rules('webname', 			'URL', 		'required|trim|min_length[1]|max_length[50]');
		$this->form_validation->set_rules('forder', 			'排序', 		'trim|integer|min_length[1]|max_length[20]');
		if ($this->form_validation->run() == FALSE){
   			$this->load->view('/ychadmin/links_add');
   			return;
  		}
  		
  		$data['type'] 		= $type;
  		$data['url'] 		= $url;
  		$data['webname'] 	= $webname;
  		$data['forder'] 	= $forder;
  		$data['logoimg'] 	= $logoimg;
  		$data['add_time'] 	= time();
  		$data['addip'] 		= $this->js->get_ip();
  		$this->load->model('links_model');
  		$links_id = $this->links_model->add($data);
  		
  		header("location:/ychadmin/links/linkslist");
		$this->load->model('log_model');$this->log_model->add_log('[添加][友情链接]['.$webname.']');
   		exit;
	}
	
	function view($links_id){
		$this->load->model('links_model');
  		$links = $this->links_model->get_links_obj($links_id);
  		$vdata['links'] = $links;
  		
  		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'links';
		
  		$this->load->view("/ychadmin/links_view", $vdata);
	}
	
	function edit_view($links_id){
		$this->load->helper(array('form'));
		$this->load->model('links_model');
  		$links = $this->links_model->get_links_obj($links_id);
  		$vdata['links'] = $links;
  		
  		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'links';
		
  		$this->load->view("/ychadmin/links_edit", $vdata);
	}
	
	function edit(){
		$id 		= $this->input->post('id');
		$type 		= $this->input->post('type');
		$url 		= $this->input->post('url');
		$webname 	= $this->input->post('webname');
		$forder 	= $this->input->post('forder');
		
		$blogoimg 		= $this->input->post('blogoimg');
		$logoimg = "";
		if($_FILES['logoimg']['name'] != ''){ 
			$logoimg = sha1($_SERVER['HTTP_USER_AGENT'].$_SERVER['REMOTE_ADDR'].time().rand());
			$tmpfn = $_FILES['logoimg']['name'];
			$pt=strrpos($tmpfn, ".");
			$retval=substr($tmpfn, $pt+1, strlen($tmpfn) - $pt);
			$logoimg.=".".$retval;
			$upfile = UPFILES_LINKS_IMG."/".$logoimg;
			if(is_uploaded_file($_FILES['logoimg']['tmp_name']))
				move_uploaded_file($_FILES['logoimg']['tmp_name'],$upfile);
		}
		$logoimg = $logoimg?$logoimg:$blogoimg;
		
		$this->load->helper(array('form'));
		$this->load->library('form_validation');
		$this->form_validation->set_rules('id', 				'ID', 		'required');
		$this->form_validation->set_rules('url', 				'标题', 		'trim|min_length[1]|max_length[100]');	
		$this->form_validation->set_rules('webname', 			'URL', 		'required|trim|min_length[1]|max_length[50]');
		$this->form_validation->set_rules('forder', 			'排序', 		'trim|integer|min_length[1]|max_length[20]');
		if ($this->form_validation->run() == FALSE){
   			$this->load->view('/ychadmin/links_add');
   			return;
  		}
  		
  		$data['type'] 		= $type;
  		$data['url'] 		= $url;
  		$data['webname'] 	= $webname;
  		$data['forder'] 	= $forder;
  		$data['logoimg'] 	= $logoimg;
  		$this->load->model('links_model');
  		$links_id = $this->links_model->update($id, $data);
  		
  		header("location:/ychadmin/links/linkslist");
		$this->load->model('log_model');$this->log_model->add_log('修改][友情链接]ID=='.$id);
   		exit;
	}
	
	function linkslist(){
		$this->load->model('links_model');
		
		$webname 	= $this->input->get('webname');
		$type	= $this->input->get('type');
		
		if($webname != ''){
			$data['where']['webname'] = $webname;
		}
		
		if( $type !=''){
			$data['where']['type'] = $type;
		}
		
		$vdata = $this->links_model->get_links_list($data);
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/administrator/links/linkslist/?webname='.$webname.'&type='.$type;
		$config['total_rows'] = $vdata['links_count'];
		$config['per_page'] = ADMINGSDAI_LIST_PAGE_SIZE; 
		$this->pagination->initialize($config); 
		
		$vdata['webname'] 	= $webname;
		$vdata['type'] 		= $type;
		
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'links';
		
		$this->load->view('/ychadmin/links_list', $vdata);
	}
	
	function remove(){
		$id 				= $this->input->post('id');
  		$data["is_del"]		= 1;
  		$this->load->model('links_model');
  		$this->load->model('log_model');$this->log_model->add_log('[删除][友情链接]');
		echo  $this->links_model->update($id, $data);
	}
	
}
